/*
 * @Author: heinan
 * @Date: 2023-07-23 22:51:06
 * @Last Modified by: yaozixian
 * @Last Modified time: 2023-08-09 14:30:03
 */
"use strict";
const {
  Controller
} = require("egg");

/**
 *  @Controller 房源管理
 */
class Housing extends Controller {
  /**
   * @Summary 租房查询接口
   * @Description 租房查询接口
   * @Router GET /housing
   * @Request query string area 区域
   * @Request query string name 房名
   * @Request query string id
   * @Request query string currentPage 页码 默认为1
   * @Request query string pageSize 数量 默认为10
   * @Request query number status 房源状态
   * @Request query string toword 朝向
   * @Request query string house_type 房型
   * @Request query string price 价格区间 json字符串'price:{min?:string,max:string}'
   * 
   */
  async housing() {
    //get
    // console.log(this.ctx.query,"00000000");
    const result = await this.ctx.service.housing.index(this.ctx.query);
    // let data = []
    // console.log(this.ctx.query)
    // if (this.ctx.query.page && this.ctx.query.pageSize) {
    //   data = result.length.splice(
    //     (this.ctx.query.page - 1) * (this.ctx.query.pageSize * 1),
    //     this.ctx.query.pageSize * 1
    //   )
    // }
    // console.log(result);
    if (result.data.length) {
      this.ctx.body = {
        code: 1,
        msg: "查询成功！",
        // data2: data,
        ...result,
      };
    } else {
      this.ctx.body = {
        code: 0,
        msg: "暂无数据！",
        data: [],
      };
    }
  }

  /**
   * @Summary 删除租房房源接口
   * @Description 删除房源管理销售楼盘数据接口
   * @Router DELETE /deleteMarkethouses/{id}
   * @Request path string *id
   */
  async deleteMarkethouses() {
    // console.log(this.ctx.params, "88888888888888888888")
    const result = await this.ctx.service.housing.destroy(this.ctx.params);
    if (result.affectedRows) {
      this.ctx.body = {
        code: 1,
        message: "删除成功！",
      };
    } else {
      this.ctx.body = {
        code: 0,
        message: "删除失败！",
      };
    }
  }

  /**
   * @Summary 修改销售楼盘状态接口
   * @Description 修改销售楼盘状态接口
   * @Router POST /setMarketHouseStatus
   * @Request body setHouseStatus *body
   */
  async setMarketHouseStatus() {
     console.log( this.ctx.request.body,11111);
    const result = await this.ctx.service.housing.update(this.ctx.request.body);
    if (result.affectedRows) {
      this.ctx.body = {
        code: 1,
        message: "修改成功！",
      };
    } else {
      this.ctx.body = {
        code: 0,
        message: "修改失败！",
      };
    }
  }

  /**
   * @Summary 修改房源管理页面销售经纪人数据接口
   * @Description 修改房源管理页面销售经纪人数据接口
   * @Router POST /alterMarketBroker
   * @Request body setHouseBroker *body
   */
  async alterMarketBroker() {
   
    const result = await this.ctx.service.housing.updateBroker(
      this.ctx.request.body
    );
    if (result.affectedRows) {
      this.ctx.body = {
        code: 1,
        message: "修改成功！",
      };
    } else {
      this.ctx.body = {
        code: 0,
        message: "修改失败！",
      };
    }
  }
  /**
   * @Summary 添加租房房源接口
   * @Description 添加租房房源接口
   * @Router POST /addHouse
   * @Request body createHouseRequest *body
   */
  async addHouses() {
    const result = await this.ctx.service.housing.create(this.ctx.request.body);
    if (result.affectedRows) {
      this.ctx.body = {
        code: 1,
        message: "添加成功！",
      };
    } else {
      this.ctx.body = {
        code: 0,
        message: "添加失败！",
      };
    }
  }
  /**
   * @Summary 修改租房房源接口
   * @Description 修改租房房源接口
   * @Router POST /editHouses
   * @Request body updateHouseRequest *body
   */
  async editHouses() {
    // console.log(this.ctx.request.body);
    const result = await this.ctx.service.housing.editHouse(
      this.ctx.request.body
    );
    if (result.affectedRows) {
      this.ctx.body = {
        code: 1,
        message: "修改成功！",
      };
    } else {
      this.ctx.body = {
        code: 0,
        message: "修改失败！",
      };
    }
  }

  /**
   * @Summary 获取二手房房源
   * @Description 获取二手房房源
   * @Router GET /second/housing
   * @Request query string area 区域
   * @Request query string name 房名
   * @Request query string id
   * @Request query string currentPage 页码 默认为1
   * @Request query string pageSize 数量 默认为10
   * @Request query number status 房源状态
   * @Request query string toword 朝向
   * @Request query string house_type 房型
   * @Request query string price 价格区间 json字符串'price:{min?:string,max:string}'
   */
  async secondHousing() {
    const result = await this.ctx.service.housing.secondIndex(this.ctx.query);
    if (result.data.length) {
      this.ctx.body = {
        code: 1,
        msg: "查询成功！",
        ...result,
      };
    } else {
      this.ctx.body = {
        code: 0,
        msg: "暂无数据！",
        data: [],
      };
    }
  }

  /**
   * @Summary 删除二手房数据接口
   * @Description 删除二手房数据接口
   * @Router DELETE /deleteRenthouses/{id}
   * @Request path string *id
   */
  async deleteRenthouses() {
    const result = await this.ctx.service.housing.secondDestroy(this.ctx.params);
    if (result.affectedRows) {
      this.ctx.body = {
        code: 1,
        message: "删除成功！",
      };
    } else {
      this.ctx.body = {
        code: 0,
        message: "删除失败！",
      };
    }
  }

  /**
   * @Summary 修改二手房源状态接口
   * @Description 修改二手房源状态接口
   * @Router POST /setRentHouseStatus
   * @Request body setSecondHouseStatus *body
   */
  async setRentHouseStatus() {
    const result = await this.ctx.service.housing.secondUpdate(
      this.ctx.request.body
    );
    if (result.affectedRows) {
      this.ctx.body = {
        code: 1,
        message: "修改成功！",
      };
    } else {
      this.ctx.body = {
        code: 0,
        message: "修改失败！",
      };
    }
  }
  /**
   * @Summary 修改二手房源经纪人接口
   * @Description 修改二手房源经纪人接口
   * @Router POST /alterRentBroker
   * @Request body setSecondHouseBroker *body
   */
  async alterRentBroker() {
    const result = await this.ctx.service.housing.updatesecondBroker(
      this.ctx.request.body
    );
    if (result.affectedRows) {
      this.ctx.body = {
        code: 1,
        message: "修改成功！",
      };
    } else {
      this.ctx.body = {
        code: 0,
        message: "修改失败！",
      };
    }
  }
  /**
   * @Summary 添加二手房房源接口
   * @Description 添加二手房房源接口
   * @Router POST /addSecondHouse
   * @Request body createSecondHouseRequest *body
   */
  async addSecondHouses() {
    const result = await this.ctx.service.housing.secondCreate(
      this.ctx.request.body
    );
    if (result.affectedRows) {
      this.ctx.body = {
        code: 1,
        message: "添加成功！",
      };
    } else {
      this.ctx.body = {
        code: 0,
        message: "添加失败！",
      };
    }
  }

  /**
   * @Summary 修改二手房房源接口
   * @Description 修改二手房房源接口
   * @Router POST /editSecondHouse
   * @Request body updateSecondHouseRequest *body
   */
  async editSecondHouses() {
    const result = await this.ctx.service.housing.editSecondHouses(
      this.ctx.request.body
    );
    if (result.affectedRows) {
      this.ctx.body = {
        code: 1,
        message: "修改成功！",
      };
    } else {
      this.ctx.body = {
        code: 0,
        message: "修改失败！",
      };
    }
  }
  /**
   * @Summary 获取租房数据
   * @Description 获取租房数据
   * @Router GET /renting
   * @Request query string area 区域
   * @Request query string name 房名
   * @Request query string id
   * @Request query string currentPage 页码 默认为1
   * @Request query string pageSize 数量 默认为10
   * @Request query number status 房源状态
   * @Request query string toword 朝向
   * @Request query string house_type 房型
   * @Request query string price 价格区间 json字符串'price:{min?:string,max:string}'
   */
  async getRenting() {
    // console.log(this.ctx.query);
    const result = await this.ctx.service.housing.getRent(this.ctx.query);
    // console.log(result);
    if (result.data.length) {
      this.ctx.body = {
        code: 1,
        msg: "查询成功！",
        ...result,
        // area,
        // typeArr,
      };
    } else {
      this.ctx.body = {
        code: 0,
        msg: "暂无数据！",
        data: [],
      };
    }
  }
}
module.exports = Housing;
